home *** CD-ROM | disk | FTP | other *** search
- /*
- *
- * $
- * $ FILE : calc.c
- * $ VERSION : 1
- * $ REVISION : 7
- * $ DATE : 08-Dec-93 18:11
- * $
- * $ Author : mvk
- * $
- *
- */
-
- /*
- Note: This is not a very good calulator, but you get the point.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <exec/types.h>
- #include <egs/egsintui.h>
- #include <egs/egsgadbox.h>
- #include <proto/exec.h>
- #include <egs/proto/all.h>
-
- /*
- ** Protos
- */
-
-
- BYTE CreateGfx(void);
- BYTE CreateWindow(void);
- void UpdateDisplay(void);
-
-
- #define ID_0 0x10
- #define ID_1 0x11
- #define ID_2 0x12
- #define ID_3 0x13
- #define ID_4 0x14
- #define ID_5 0x15
- #define ID_6 0x16
- #define ID_7 0x17
- #define ID_8 0x18
- #define ID_9 0x19
- #define ID_PLUS 0x21
- #define ID_MINUS 0x22
- #define ID_TIMES 0x23
- #define ID_DIV 0x24
- #define ID_POINT 0x31
- #define ID_CLEAR 0x32
- #define ID_EQUAL 0x33
-
-
- struct Library *EGSIntuiBase;
- struct Library *EGSGfxBase;
- struct Library *EGBBase;
-
- struct EB_GadContextNode *GadCon;
- struct EI_Window *Win;
- struct EG_EFont *Font;
-
- struct EB_InfoBox Display;
-
- double Arg1, Arg2, Res, Place;
- UBYTE Valid, Oper;
-
- BYTE CreateGfx(void)
- {
- struct EB_GadBox *root, *a1, *a2, *b1, *b2, *b3, *b4, *b6;
- BYTE ret = 0;
-
- if (GadCon = EB_CreateGadContext(NULL, NULL, -1, -1))
- {
- root = EB_CreateVertiBox(GadCon);
- EB_AddLastSon(root, EB_CreateVertiFill(GadCon, TRUE, 0));
-
- a1 = EB_CreateHorizBox(GadCon);
- EB_AddLastSon(root, a1);
- EB_AddLastSon(a1, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(a1, EB_CreateInfoBox(GadCon, &Display, Font, 6, 12, EI_JUSTIFY_RIGHT));
- EB_AddLastSon(a1, EB_CreateHorizFill(GadCon, TRUE, 0));
-
- EB_AddLastSon(root, EB_CreateVertiFill(GadCon, TRUE, 0));
-
- a2 = EB_CreateVertiTable(GadCon);
- EB_AddLastSon(root, a2);
-
- b6 = EB_CreateHorizBox(GadCon);
- EB_AddLastSon(a2, b6);
- EB_AddLastSon(b6, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b6, EB_CreateTextAction(GadCon, " ", 0, EB_FILL_ALL));
- EB_AddLastSon(b6, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b6, EB_CreateTextAction(GadCon, " ", 0, EB_FILL_ALL));
- EB_AddLastSon(b6, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b6, EB_CreateTextAction(GadCon, " ", 0, EB_FILL_ALL));
- EB_AddLastSon(b6, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b6, EB_CreateTextAction(GadCon, "C", ID_CLEAR, EB_FILL_ALL));
- EB_AddLastSon(b6, EB_CreateHorizFill(GadCon, TRUE, 0));
-
- EB_AddLastSon(a2, EB_CreateVertiFill(GadCon, TRUE, 0));
-
- b1 = EB_CreateHorizBox(GadCon);
- EB_AddLastSon(a2, b1);
- EB_AddLastSon(b1, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b1, EB_CreateTextAction(GadCon, "7", ID_7, EB_FILL_ALL));
- EB_AddLastSon(b1, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b1, EB_CreateTextAction(GadCon, "8", ID_8, EB_FILL_ALL));
- EB_AddLastSon(b1, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b1, EB_CreateTextAction(GadCon, "9", ID_9, EB_FILL_ALL));
- EB_AddLastSon(b1, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b1, EB_CreateTextAction(GadCon, "/", ID_DIV, EB_FILL_ALL));
- EB_AddLastSon(b1, EB_CreateHorizFill(GadCon, TRUE, 0));
-
- EB_AddLastSon(a2, EB_CreateVertiFill(GadCon, TRUE, 0));
-
- b2 = EB_CreateHorizBox(GadCon);
- EB_AddLastSon(a2, b2);
- EB_AddLastSon(b2, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b2, EB_CreateTextAction(GadCon, "4", ID_4, EB_FILL_ALL));
- EB_AddLastSon(b2, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b2, EB_CreateTextAction(GadCon, "5", ID_5, EB_FILL_ALL));
- EB_AddLastSon(b2, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b2, EB_CreateTextAction(GadCon, "6", ID_6, EB_FILL_ALL));
- EB_AddLastSon(b2, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b2, EB_CreateTextAction(GadCon, "*", ID_TIMES, EB_FILL_ALL));
- EB_AddLastSon(b2, EB_CreateHorizFill(GadCon, TRUE, 0));
-
- EB_AddLastSon(a2, EB_CreateVertiFill(GadCon, TRUE, 0));
-
- b3 = EB_CreateHorizBox(GadCon);
- EB_AddLastSon(a2, b3);
- EB_AddLastSon(b3, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b3, EB_CreateTextAction(GadCon, "1", ID_1, EB_FILL_ALL));
- EB_AddLastSon(b3, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b3, EB_CreateTextAction(GadCon, "2", ID_2, EB_FILL_ALL));
- EB_AddLastSon(b3, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b3, EB_CreateTextAction(GadCon, "3", ID_3, EB_FILL_ALL));
- EB_AddLastSon(b3, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b3, EB_CreateTextAction(GadCon, "-", ID_MINUS, EB_FILL_ALL));
- EB_AddLastSon(b3, EB_CreateHorizFill(GadCon, TRUE, 0));
-
- EB_AddLastSon(a2, EB_CreateVertiFill(GadCon, TRUE, 0));
-
- b4 = EB_CreateHorizBox(GadCon);
- EB_AddLastSon(a2, b4);
- EB_AddLastSon(b4, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b4, EB_CreateTextAction(GadCon, "0", ID_0, EB_FILL_ALL));
- EB_AddLastSon(b4, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b4, EB_CreateTextAction(GadCon, ".", ID_POINT, EB_FILL_ALL));
- EB_AddLastSon(b4, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b4, EB_CreateTextAction(GadCon, "=", ID_EQUAL, EB_FILL_ALL));
- EB_AddLastSon(b4, EB_CreateHorizFill(GadCon, TRUE, 0));
- EB_AddLastSon(b4, EB_CreateTextAction(GadCon, "+", ID_PLUS, EB_FILL_ALL));
- EB_AddLastSon(b4, EB_CreateHorizFill(GadCon, TRUE, 0));
-
- EB_AddLastSon(a2, EB_CreateVertiFill(GadCon, TRUE, 0));
-
- b1 = root;
- root = EB_CreateMaster(GadCon, 0, 0);
- EB_AddLastSon(root, b1);
- b1 = root;
- root = EB_CreateWindow(GadCon, 0, 0);
- EB_AddLastSon(root, EB_CreateBorder(GadCon, b1, 0));
-
- if (Win)
- {
- root->MinWidth = Win->Width;
- root->MaxWidth = Win->Width;
- root->MinHeight = Win->Height;
- root->MaxHeight = Win->Height;
- }
-
- if (EB_ProcessGadBoxes(GadCon, root))
- {
- ret = 1;
- }
- }
-
- return ret;
- }
-
- BYTE CreateWindow(void)
- {
- BYTE ret = 0;
-
- if (CreateGfx())
- {
- GadCon->NewWin->Title = "EGS Calculator Demo";
- GadCon->NewWin->Flags &= ~EI_SMART_REFRESH;
- GadCon->NewWin->Flags |= (EI_SIZEBBOTTOM | EI_SIMPLE_REFRESH);
- GadCon->NewWin->IDCMPFlags |= (EI_iCLOSEWINDOW | EI_iGADGETUP | EI_iSIZEVERIFY | EI_iNEWSIZE);
- GadCon->NewWin->Bordef.SysGadgets |= (EI_WINDOWCLOSE | EI_WINDOWSIZE);
- if (Win = EI_OpenWindow(GadCon->NewWin))
- {
- ret = 1;
- }
- }
-
- return ret;
- }
-
- void UpdateDisplay(void)
- {
- char buffer[64];
-
- sprintf(buffer, "%g", Res);
-
- EB_WriteInfoBox(&Display, Win, buffer);
- }
-
- void DoDigit(LONG digit)
- {
- LONG Digit;
-
- if (digit >= ID_0 && digit <= ID_9)
- {
- Digit = digit & 0x0f;
-
- if (!Valid)
- {
- Valid = 1;
- Place = 0;
- Res = 0;
- }
-
- if (Res > -100000000 && Res < 100000000)
- {
- if (Place)
- {
- Place *= 10;
- Res += (Digit / Place);
- }
- else
- {
- Res = Res * 10 + Digit;
- }
- if (Oper) Arg2 = Res;
- else Arg1 = Res;
- }
-
- UpdateDisplay();
- }
- }
-
- void DoArithmetic(LONG arith)
- {
- if (arith >= ID_PLUS && arith <= ID_DIV)
- {
- Oper = arith;
- Arg2 = Res;
- Valid = 0;
- UpdateDisplay();
- }
- }
-
- void DoSpecial(LONG special)
- {
- if (special >= ID_POINT && special <= ID_EQUAL)
- {
- if (special == ID_POINT)
- {
- if (!Valid)
- {
- Valid = 1;
- Place = 0;
- Res = 0;
- }
- if (!Place) Place = 1;
- }
-
- if (special == ID_CLEAR)
- {
- Res = 0;
- Arg1 = 0;
- Arg2 = 0;
- Valid = 0;
- Place = 0;
- }
-
- if (special == ID_EQUAL)
- {
- if (Oper == ID_PLUS) Res = Arg1 + Arg2;
- if (Oper == ID_MINUS) Res = Arg1 - Arg2;
- if (Oper == ID_TIMES) Res = Arg1 * Arg2;
- if (Oper == ID_DIV) Res = Arg1 / Arg2;
- Arg1 = Res;
- Oper = 0;
- Valid = 0;
- }
- UpdateDisplay();
- }
- }
-
- main()
- {
- struct EI_EIntuiMsg *IMsg;
- struct EI_Gadget *TempGad;
- BYTE quit = 0;
-
- if (EGSIntuiBase = OpenLibrary("egsintui.library", 0))
- {
- if (EGSGfxBase = OpenLibrary("egsgfx.library", 0))
- {
- if (EGBBase = OpenLibrary("egsgadbox.library", 0))
- {
- if (Font = EG_OpenFont(EI_GetPrefFont(1)))
- {
- if (CreateWindow())
- {
- UpdateDisplay();
- while (!quit)
- {
- WaitPort(Win->UserPort);
- if (IMsg = (struct EI_EIntuiMsg *)GetMsg(Win->UserPort))
- {
- if (IMsg->Class == EI_iCLOSEWINDOW)
- {
- quit = 1;
- }
- if (IMsg->Class == EI_iGADGETUP)
- {
- if (TempGad = (struct EI_Gadget *)IMsg->IAddress)
- {
- if ((TempGad->GadgetID & 0xf0) == 0x10)
- {
- DoDigit(TempGad->GadgetID);
- }
- if ((TempGad->GadgetID & 0xf0) == 0x20)
- {
- DoArithmetic(TempGad->GadgetID);
- }
- if ((TempGad->GadgetID & 0xf0) == 0x30)
- {
- DoSpecial(TempGad->GadgetID);
- }
- }
- }
- if (IMsg->Class == EI_iSIZEVERIFY)
- {
- EI_RemoveGList(Win, GadCon->First, GadCon->Num);
- EB_DeleteGadContext(GadCon);
- GadCon = NULL;
- }
- if (IMsg->Class == EI_iNEWSIZE)
- {
- EI_LockIntuition();
- CreateGfx();
- if (GadCon)
- {
- EI_AddGList(Win, GadCon->First, GadCon->Num);
- }
- EI_UnlockIntuition();
- UpdateDisplay();
- }
- ReplyMsg((struct Message *)IMsg);
- }
- }
- }
- EG_CloseFont(Font);
- }
- if (Win) EI_CloseWindow(Win);
- if (GadCon) EB_DeleteGadContext(GadCon);
- CloseLibrary(EGBBase);
- }
- CloseLibrary(EGSGfxBase);
- }
- CloseLibrary(EGSIntuiBase);
- }
- }
-